matplotlib axis
Documentation
https://gyazo.com/68fd429e8e4ead6d5d09bfe4a9b9e492
Wiki/Summary
memoring: matplotlib 軸の設定
hr.icon
Title
adjust vertical position of title
code:python
ax.set_title("title",y=0.85)
Python Matplotlib figure title overlaps axes label when using twiny
hr.icon
XY axes
Invert
軸を逆転
code:python
ax=gca()
ax.invert_xaxis()
ax.invert_yaxis()
log scale
code:python
ax = gca()
ax.set_xscale('log')
ax.set_yscale('log')
Range
code:python
Erase axis
軸を消す
code:python
ax.axis('off')
Multiple y-axis scales
hr.icon
Ticks
location of ticks
code:python
import matplotlib.ticker as ticker
xmajorLocator=ticker.FixedLocator(np.arange(-2,3,0.4))
ax.xaxis.set_major_locator( xmajorLocator )
Finds up to a max number of intervals with ticks at nice locations
tickの間隔の個数の最大値を指定する
code:python
import matplotlib.ticker as ticker
ax=plt.gca()
locator=ticker.MaxNLocator(4)
ax.xaxis.set_major_locator(locator)
code:python
plt.yticks(np.arange(-25,80, step=50))
Change tick length and width
code:python
ax.xaxis.set_tick_params(width=2)
code:python
ax.tick_params(axis='x', which='major',length=1,width=1, pad=0.5)
ax.tick_params(axis='y', which='major',length=1,width=1, pad=0.5)
colorbarでも使える
code:python
for line in ax.get_xticklines() + ax.get_yticklines():
line.set_markersize(5)
line.set_markeredgewidth(2)
inside out
tickを外に出す
code:python
for line in ax.get_xticklines() + ax.get_yticklines():
line.set_markersize(-5) # negative values
hr.icon
Tick lables
Specify digits of tick labels
code:python
import matplotlib.ticker as ticker
ax=plt.gca()
formatter=ticker.FormatStrFormatter('%3.2f')
ax.xaxis.set_major_formatter(formatter)
Using characters for tick labels
tickの位置に文字列
code:python
import matplotlib.ticker as ticker
xmajorLocator=ticker.FixedLocator(np.arange(1,13))
ax.xaxis.set_major_locator(xmajorLocator)
ax.xaxis.set_ticklabels("J","F","M","A","M","J","J","A","S","O","N","D") Change tick label size
code:python
ax.xaxis.set_tick_params(width=2)
Erase tick labels
軸の数字を消す
code:python
ax.set_yticklabels([])
ax.set_xticklabels([])
Writing mathematical expressions
font size
code: python
item.set_fontsize(24)
hr.icon
rcParms
matplotlibの軸や凡例の文字を大きくするなどの設定
code:python
params = {
'font.family' :'serif',
'font.size' :14,
'axes.labelsize' :14,
'xtick.labelsize':14,
'ytick.labelsize':14,
'legend.fontsize':14
}
plt.rcParams.update(params)
hr.icon
Matplotlib AxesGrid Toolkit
hr.icon
axes.Axes.set
code:python
ax.set(xlabel="X",ylabel="Y",title="Title",xlim=0,1, ylim=2,3, autoscale_on=False) matplotlib.axes.Axes.set
hr.icon
inset figure
hr.icon
Examples
Create a plot with broken axis
Scenario: Say we have profile data from the deep ocean. The profile is for the full depth of the ocean but most of the signal is in the upper ocean. We want to make a plot of the full profile but have a zoomed in look at the upper ocean. We can do this by manipulating the subplot feature.
sharing x/y axes (x,y軸の範囲をshareするsubplot)
Matplotlib: Changing the color of an axis - Stack Overflow
MetPy Mondays `#151 - Eliminating Chart Junk : Unidata Developer's Blog code:python
ax.spines'top'.set_visible(False) ax.get_xaxis().tick_bottom()
ax.grid(line_style="--",alpha=0.5)
li, = ax.plot(times,t1)
plt.tick_params(axis="both",which="both",bottom="False",top="False"
,labelbottom=True,left=False,right=False,labelleft=True)
ax.text(times-1,ti-1,"KTUL",color=li.get_color(),fontsize=16) https://gyazo.com/d0088d99a398408aa8fe4723cca6f9e4
hr.icon
See also